<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="name1" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model="age1" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="sex1">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="addBtn">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, index) in list" :key="index">
          <td>{{ index + 1 }}</td>
          <td>{{ item.uname }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.sex }}</td>
          <td>
            <button @click="delBtn(index)">删除</button>
            <button>编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [
        { uname: 'Tom2', age: 25, sex: '女' },
        { uname: 'Jone', age: 21, sex: '女' },
        { uname: '小李', age: 18, sex: '男' },
      ],
      name1: '',
      age1: '',
      sex1: '',
    }
  },
  methods: {
    //添加数据
    addBtn() {
      if (this.name1.trim() === 0 || this.age1 <= 0 || this.sex1.length <= 0) {
        alert('输入的格式不正确')
        return
      }
      this.list.push({
        uname: this.name1,
        age: this.age1,
        sex: this.sex1,
      })
      this.name1 = ''
      this.age1 = ''
    },
    //编辑

    //删除数据
    delBtn(index) {
      this.list.splice(index, 1)
    },
  },
}
</script>